package com.eboss.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.adapter.RangeListAdapter;
import com.eboss.sbs.enums.EnumConsts;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.PointDetailEntity;
import com.eboss.sbs.model.RangeEntity;
import com.google.gson.reflect.TypeToken;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.views.MyPictureDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PointActivateDetailActivity extends BaseActivity {
	
	private int id;
	private PointDetailEntity myData;
	
	private ListView lv_range;
	private boolean lv_range_show = true;
	private List<RangeEntity> datas = new ArrayList<RangeEntity>();
	private RangeListAdapter adapter;
	
	private ImageView arrow_rangType;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_point_detail);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "活动详情 ");
		
		id = getIntent().getIntExtra("id", -1);
		
		lv_range = (ListView) findViewById(R.id.lv_range);
		adapter = new RangeListAdapter(mActivity, datas);
		lv_range.setAdapter(adapter);
		
		arrow_rangType = (ImageView) findViewById(R.id.arrow_rangType);
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		findViewById(R.id.layout_stores).setOnClickListener(this);
		findViewById(R.id.layout_rangType).setOnClickListener(this);
		findViewById(R.id.tv_picture_show).setOnClickListener(this);
		findViewById(R.id.layout_webdetail).setOnClickListener(this);
		
		getdata();
	}
	
	private void getdata(){
		datas.clear();
		adapter.notifyDataSetChanged();
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("id", id);
		
		API.http_post(this, API.POINT_DETAIL, params, new HttpCallback<PointDetailEntity>(this) {
			@Override
			public void onSuccess(int statusCode, PointDetailEntity data) {
				
				myData = data;
				
				List<RangeEntity> tmp_datas = data.getBaseRanges();
				if(tmp_datas!=null&&tmp_datas.size()>0){
					datas.addAll(tmp_datas);
					adapter.notifyDataSetChanged();
				}
				
				setTvText(R.id.tv_name, data.getName());
				setTvText(R.id.tv_state, EnumConsts.PointState.getByState(data.getState()).getName());
				setTvText(R.id.tv_period, data.getPeriod());
				setTvText(R.id.tv_stores, data.getStoreNum()+"家");
				if(lv_range_show)arrow_rangType.setImageResource(R.mipmap.ic_arrow_up);
				setTvText(R.id.tv_rangType, data.getRangeType());
				setTvText(R.id.tv_method, data.getMethod());
				setTvText(R.id.tv_level, data.getLevel());
				showView(R.id.loyout_relative, !StringUtil.isNull(data.getRelated()));
				setTvText(R.id.tv_relative, data.getRelated());

			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<PointDetailEntity>() {
				};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.layout_webdetail:
				Intent intent0 = new Intent(mActivity, WebViewActivity.class);
				intent0.putExtra(WebViewActivity.WEBVIEW_TITLE, "活动详情");
				intent0.putExtra(WebViewActivity.HTTP_CONTENT, myData.getWebdetail());
				startActivity(intent0);
				break;
			case R.id.layout_stores:
				Intent intent = new Intent(mActivity, PointActvateShopListAcvitiy.class);
				intent.putExtra("id", myData.getId());
				startActivity(intent);
				break;
			case R.id.layout_rangType:
				lv_range_show = !lv_range_show;
				lv_range.setVisibility(lv_range_show?View.VISIBLE:View.GONE);
				
				arrow_rangType.setImageResource(lv_range_show?R.mipmap.ic_arrow_up:R.mipmap.ic_arrow_down);
				
				break;
			case R.id.tv_picture_show:
				showPicture("", myData.getIconUrl());
				break;
			default:break;
		}
		super.onClick(v);
	}
	
	private void showPicture(String title, String url){
		MyPictureDialog.Builder builder = new MyPictureDialog.Builder(this);
		builder.setTitle(title);
		builder.setPicture(url);
		MyPictureDialog dialog = builder.create();
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
	}

}
